﻿using DevExpress.XtraEditors;
using MesProjectWinform.Production.Common;
using MesProjectWinform.Production.Dto.Input.WorkOrder;
using MesProjectWinform.Production.Models;
using MesProjectWinform.Public;
using MesProjectWinform.Public.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MesProjectWinform.Production.Forms.WorkOrder
{
    public partial class UpdateWorkOrderForm : DevExpress.XtraEditors.XtraForm
    {
        public UpdateWorkOrderForm()
        {
            InitializeComponent();
        }
        long Id = 0;
        public void GetData(WORKORDERMODEL workorder)
        {
            // 假设workorder是从数据库或其他数据源获取的工单数据
            textEdit1.Text = workorder.WORKORDERCODE; // 工单编码
            lookUpEdit1.EditValue = workorder.WORKORDERTYPEID; // 工单类型ID
            lookUpEdit2.EditValue = workorder.WORKCUSTOMER; // 客户ID
            lookUpEdit3.EditValue = workorder.PRODUCTMASTERDATAID; // 产品主数据ID
            lookUpEdit4.EditValue = workorder.ROUTINGID; // 工艺路线ID
            lookUpEdit5.EditValue = workorder.PRODUCTIONLINEID; // 产线ID
            dateEdit1.EditValue = workorder.WORKSTARTTIME; // 计划开始时间
            dateEdit2.EditValue = workorder.WORKORDERTIME; // 计划终止时间
            textEdit2.Text = workorder.WORKORDERNUM.ToString(); // 工单数量
            memoEdit1.Text = workorder.WORKORDERREMARK;
            Id = workorder.WORKORDERID;
            radioGroup1.EditValue = workorder.WORKORDERISNO; // 设置是否启用的选中状态
        }
        private void UpdateForm_Load(object sender, EventArgs e)
        {
            #region 初始化工单类型下拉框
            //为lookUpEdit1绑定数据源并且设置分开设置显示的字段和实际存储的字段
            lookUpEdit1.Properties.DataSource = new List<TypeModel>()
            {
                //Id看情况加，如果Id是用来作为Value的话就加
                new TypeModel(){Name="正常"},
                new TypeModel(){Name="维修"},
                new TypeModel(){Name="委外"},
                new TypeModel(){Name="自建工单"}
            };
            lookUpEdit1.Properties.DisplayMember = "Name"; // 显示的字段
            lookUpEdit1.Properties.ValueMember = "Name"; // 实际存储的字段
            //在显示时只显示Name字段
            lookUpEdit1.Properties.PopulateColumns();
            lookUpEdit1.Properties.Columns[0].Visible = false; // 隐藏Id字段
            //下拉框启动时默认显示请选择
            lookUpEdit1.Properties.NullText = "请选择工单类型";
            #endregion 

            #region 初始化客户下拉框
            lookUpEdit2.Properties.DataSource = new List<TypeModel>()
            {
                new TypeModel(){Id=1,Name="上海明珠有限公司"},
                new TypeModel(){Id=2,Name="北京谱系有限公司"},
                new TypeModel(){Id=2,Name="江苏五力有限公司"},
                new TypeModel(){Id=2,Name="杭州利明有限公司"},
            };
            lookUpEdit2.Properties.DisplayMember = "Name"; // 显示的字段
            lookUpEdit2.Properties.ValueMember = "Name"; // 实际存储的字段

            //在显示时只显示Name字段
            lookUpEdit2.Properties.PopulateColumns();
            lookUpEdit2.Properties.Columns[0].Visible = false; // 隐藏Id字段

            //下拉框启动时默认显示请选择
            lookUpEdit2.Properties.NullText = "请选择客户";
            #endregion

            #region 初始化产品主数据下拉框
            lookUpEdit3.Properties.DataSource = new List<TypeModel>()
            {
                new TypeModel(){Id=1,Name="塑料外壳"},
                new TypeModel(){Id=2,Name="铝合金骨架"}
            };
            lookUpEdit3.Properties.DisplayMember = "Name"; // 显示的字段
            lookUpEdit3.Properties.ValueMember = "Id"; // 实际存储的字段

            //在显示时只显示Name字段
            lookUpEdit3.Properties.PopulateColumns();
            lookUpEdit3.Properties.Columns[0].Visible = false; // 隐藏Id字段

            //下拉框启动时默认显示请选择
            lookUpEdit3.Properties.NullText = "请选择产品主数据";
            #endregion

            #region 初始化工艺路线下拉框
            lookUpEdit4.Properties.DataSource = new List<TypeModel>()
            {
                new TypeModel(){Id=1,Name="部件组装与测试"},
                new TypeModel(){Id=2,Name="整机总装与测试"},
                new TypeModel(){Id=3,Name="零件加工与成型"},
                new TypeModel(){Id=4,Name="原材料与供应链管理"},
            };
            lookUpEdit4.Properties.DisplayMember = "Name"; // 显示的字段
            lookUpEdit4.Properties.ValueMember = "Id"; // 实际存储的字段

            //在显示时只显示Name字段
            lookUpEdit4.Properties.PopulateColumns();
            lookUpEdit4.Properties.Columns[0].Visible = false; // 隐藏Id字段

            //下拉框启动时默认显示请选择
            lookUpEdit4.Properties.NullText = "请选择工艺路线";
            #endregion

            #region 初始化产线下拉框
            lookUpEdit5.Properties.DataSource = new List<TypeModel>()
            {
                new TypeModel(){Id=1,Name="外壳加工"},
                new TypeModel(){Id=2,Name="骨架加工"},
                new TypeModel(){Id=3,Name="电路板组装"},
                new TypeModel(){Id=4,Name="电池组加工"},
                new TypeModel(){Id=5,Name="机体组装"},
                new TypeModel(){Id=6,Name="动力系统安装"},
                new TypeModel(){Id=7,Name="控制系统安装"},
            };
            lookUpEdit5.Properties.DisplayMember = "Name"; // 显示的字段
            lookUpEdit5.Properties.ValueMember = "Id"; // 实际存储的字段

            //在显示时只显示Name字段
            lookUpEdit5.Properties.PopulateColumns();
            lookUpEdit5.Properties.Columns[0].Visible = false; // 隐藏Id字段

            //下拉框启动时默认显示请选择
            lookUpEdit5.Properties.NullText = "请选择产线";
            #endregion
        }

        private async void simpleButton1_Click(object sender, EventArgs e)
        {
            if (lookUpEdit1.EditValue == null)
            {
                MessageBox.Show("请选择工单类型");
                return;
            }
            if (lookUpEdit2.EditValue == null)
            {
                MessageBox.Show("请选择客户");
                return;
            }
            if (lookUpEdit3.EditValue == null)
            {
                MessageBox.Show("请选择产品主数据");
                return;
            }
            if (lookUpEdit4.EditValue == null)
            {
                MessageBox.Show("请选择工艺路线");
                return;
            }
            if (lookUpEdit5.EditValue == null)
            {
                MessageBox.Show("请选择产线");
                return;
            }
            UpdateWorkorderInput workorder = new UpdateWorkorderInput()
            {
                WORKORDERTYPEID = (lookUpEdit1.GetSelectedDataRow() as TypeModel).Name, // 获取选中的工单类型ID
                WORKCUSTOMER = (lookUpEdit2.GetSelectedDataRow() as TypeModel).Name, // 获取选中的客户名称
                PRODUCTMASTERDATAID = (lookUpEdit3.GetSelectedDataRow() as TypeModel).Id, // 获取选中的产品主数据ID
                ROUTINGID = (lookUpEdit4.GetSelectedDataRow() as TypeModel).Id, // 获取选中的工艺路线ID
                PRODUCTIONLINEID = (lookUpEdit5.GetSelectedDataRow() as TypeModel).Id, // 获取选中的产线ID
                WORKSTARTTIME = dateEdit1.DateTime, // 获取实际开始时间
                WORKORDERTIME = dateEdit2.DateTime, // 获取实际结束时间
                WORKORDERISNO = (bool)radioGroup1.Properties.Items[radioGroup1.SelectedIndex].Value,
                WORKORDERID = Id, // 使用传入的Id
                WORKORDERNUM = Convert.ToInt32(textEdit2.Text),
                WORKORDERANNEX = "string",
                WORKORDERREMARK = memoEdit1.Text,
            };

            using (var client = new HttpClient())
            {
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(workorder);
                var response = await HttpClientHelper.RequestUrl(RequestType.Post, ProductionGolab.UpdateWorkOrder, json);
                var result = Newtonsoft.Json.JsonConvert.DeserializeObject<ApiResult<int>>(response);
                
                if (result.Code == ApiEnums.Success)
                {
                    this.DialogResult = DialogResult.OK; // 设置对话框结果为OK
                    this.Close(); // 关闭当前窗体
                    MessageBox.Show(result.Message);
                }
                else
                {
                    MessageBox.Show(result.Message);
                }
            }
        }
    }
}